package com.example.demo.service;

import com.example.demo.mapper.ArticleMapper;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.pojo.Article;
import com.example.demo.pojo.Category;
import com.example.demo.pojo.Link;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    public CategoryMapper categoryMapper;
    @Autowired
    public ArticleMapper articleMapper;

    public Category select(int id){
      Category  category=categoryMapper.selectByPrimaryKey(id);
        return category;
    }

    public void insert(Category category){
        categoryMapper.insert(category);
    }

    public void delete(int id){
        Example example=new Example(Article.class);
        example.createCriteria().andEqualTo("category_id",id);
        articleMapper.deleteByExample(example);//也要删除对应的Arti表里的数据

        categoryMapper.deleteByPrimaryKey(id);
    }

    public void update(Category category){
        categoryMapper.updateByPrimaryKey(category);
    }

    public PageInfo findByPage(int size, int page){
        PageHelper.startPage(page,size);//分页

        List<Category> categories = categoryMapper.selectAll();//查询指定页码的数据

        PageInfo PageInfo = new PageInfo<>(categories);//将数据封装
        return PageInfo;
    }
}
